<template><!--
columns : 必选, 列描述数据对象， Array
dataSource : 必选, 数据数组
options : 必选, 表格参数控制, maxHeight、stripe 等等..
fetch : 获取数据的 Function
pagination : 分页信息，不传则不显示分页
row-click ：当某一行被点击时会触发该事件
selection-change : 当选择项发生变化时会触发该事件
-->
  <div class="base-table-wrap">
    <el-table
      v-loading="options.loading"
      :data="dataSource"
      :max-height="options.maxHeight"
      :stripe="options.stripe"
      :border="options.border"
      @row-click="handleRowClick"
      @selection-change="handleSelectionChange"
      header-row-class-name="table-header-row">
      <!--折叠-->
      <el-table-column type="expand" v-if="options.expand">
        <template slot-scope="scope">
          <slot name="expand" :row="scope"></slot>
        </template>
      </el-table-column>

      <!--selection选择框-->
      <el-table-column v-if="options.mutiSelect" type="selection" style="width:50px" align="center" />
      <!--序号-->
      <el-table-column v-if="options.index" label="序号" type="index" width="50" align="center" />

      <!--数据列-->
      <template v-for="(column, index) in columns">
        <el-table-column
          :key="index"
          :prop="column.prop"
          :label="column.label"
          :align="column.align||'center'"
          :width="column.width"
          :minWidth="column.minWidth"
          :fixed="column.fixed">
          <template slot-scope="scope">

            <template v-if="!column.render">
              {{scope.row[column.prop]}}
            </template>

            <!-- render -->
            <template v-else>
              <RenderDom :row="scope.row" :index="index" :render="column.render" />
            </template>

            <!-- render button -->
            <template v-if="column.button">
               <!-- 下拉按钮 -->
              <template v-for="(btn, i) in column.group">
                <el-dropdown v-if="btn.isOption" :key="i">
                  <el-button :type="btn.type"
                    :size="btn.size || 'mini'"
                    :icon="btn.icon"
                    :disabled="btn.disabled"
                    :plain="btn.plain"
                    :style="btn.style">{{btn.name}}</el-button>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item v-for='item in btn.optionsBtn' :key="item.value">
                      <el-button type='text' @click="btn.onClick(scope.row, scope.$index, item.value)">{{item.name}}</el-button>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
                <!-- isToggle 加入切换按钮 -->
                <el-button v-else-if="(btn.isToggle && btn.toggle(scope.row)) || !btn.isToggle"
                  :key="i"
                  :type="btn.type"
                  :size="btn.size || 'mini'"
                  :icon="btn.icon"
                  :disabled="btn.disabled"
                  :plain="btn.plain"
                  :style="btn.style"
                  @click.stop="btn.onClick(scope.row, scope.$index)"
                >{{btn.name}}</el-button>
              </template>
            </template>

          </template>

        </el-table-column>
      </template>

    </el-table>
    <div v-if="pagination" class="base-pagination clearfix">
      <slot></slot>
      <!-- 分页 -->
      <el-pagination
        :total="pagination.total"
        :page-sizes="[10, 20, 30, 50, 100, 500, 1000]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        :current-page.sync="pagination.curPage"
        @current-change="handleIndexChange"
        :pageSize="pagination.pageSize || 10"
        background />
    </div>
  </div>
</template>

<script>
  export default {
    components: {
      RenderDom: {
        functional: true, // 函数式组件 - 无 data 和 this 上下文 => better render
        props: {
          row: Object,
          index: Number,
          render: Function
        },
        /**
         * @param {Function} createElement - 原生创建dom元素的方法， 弃用，推荐使用 jsx
         * @param {Object} ctx - 渲染的节点的this对象
         * @argument 传递参数 row index
         */
        render(createElement, ctx){
          const { row, index } = ctx.props
          return ctx.props.render(row, index)
        }
      }
    },
    props:{
      dataSource: Array,
      options: Object, // 表格参数控制 maxHeight、stripe 等等...
      columns: Array,
      fetch: Function, // 获取数据的函数
      pagination: Object // 分页，不传则不显示
    },
    created() {
      // 传入的options覆盖默认设置
      this.$parent.options = Object.assign({
        maxHeight: '',
        stripe: true, // 是否为斑马纹
        border: false
      }, this.options)

      this.options.initTable && this.fetch()
    },
    methods: {
      handleSizeChange(size) { // 切换每页显示的数量
        this.pagination.size = size
        this.fetch()
      },
      handleIndexChange(current) { // 切换页码
        this.pagination.page = current - 1
        this.fetch()
      },
      handleSelectionChange(selection) { // 当选择项发生变化时会触发该事件
        this.$emit('selection-change', selection)
      },
      handleRowClick(row, event, column) { // 一行被点击时触发
        this.$emit('row-click', row, event, column)
      }
    }
  }
</script>

<style>
  .el-table th,
  .el-table tr.table-header-row {
    background: #e5c5d2; /* 示例， 对表格样式上的修饰 */
  }
</style>
